package com.xiaoxin.experience.util;

import java.util.*;

/**
 * @author xiaoxin
 */
public class TreeUtil
{

    public static List<TreeNode> build(List<TreeNode> treeNodes, String rootId)
    {
        Map<String,TreeNode> nodeMap = new HashMap<>();
        for (TreeNode treeNode : treeNodes)
        {
            nodeMap.put(treeNode.getId(), treeNode);
        }

        for (TreeNode treeNode : treeNodes)
        {
            String parentId = treeNode.getParentId();
            TreeNode node = nodeMap.get(parentId);
            if (Objects.nonNull(node))
            {
                node.addChild(treeNode);
            }
        }
        return nodeMap.get(rootId).getChildren();
    }

    public static void main(String[] args) {

        TreeNode treeNode1 = new TreeNode("1","广州","0");
        TreeNode treeNode2 = new TreeNode("2","深圳","0");

        TreeNode treeNode3 = new TreeNode("3","天河区",treeNode1);
        TreeNode treeNode4 = new TreeNode("4","越秀区",treeNode1);
        TreeNode treeNode5 = new TreeNode("5","黄埔区",treeNode1);
        TreeNode treeNode6 = new TreeNode("6","石牌",treeNode3);
        TreeNode treeNode7 = new TreeNode("7","百脑汇",treeNode6);


        TreeNode treeNode8 = new TreeNode("8","南山区",treeNode2);
        TreeNode treeNode9 = new TreeNode("9","宝安区",treeNode2);
        TreeNode treeNode10 = new TreeNode("10","科技园",treeNode8);


        List<TreeNode> list = new ArrayList<TreeNode>();

        list.add(treeNode1);
        list.add(treeNode2);
        list.add(treeNode3);
        list.add(treeNode4);
        list.add(treeNode5);
        list.add(treeNode6);
        list.add(treeNode7);
        list.add(treeNode8);
        list.add(treeNode9);
        list.add(treeNode10);

        List<TreeNode> trees = build(list,"2");
        System.out.println(trees);

    }
}
